﻿using System;
using System.Text;
using SkeinLib;
namespace System.Security.Cryptography
{
    public class Skein256 : System.Security.Cryptography.HashAlgorithm
    {
        private const int WORDWIDTH = 4;
        private const int DEFAULTHASHBITLENGTH = 256;

        internal byte[] _hashResult = null;
        internal UInt64[] _localState = null;

        public override byte[] Hash { get { return _hashResult; } }
        public override int HashSize { get { return DEFAULTHASHBITLENGTH; } }

        public new byte[] ComputeHash(byte[] data)
        {
            if (_localState == null)
                throw new InvalidOperationException("HashAlgorithm not initialized.  Call Initialize first!");
            if (data == null)
                throw new ArgumentNullException("data");
            SkeinCore.ProcessMessage(ref _localState, data);
            _hashResult = SkeinCore.ProcessStandardOutput(ref _localState, DEFAULTHASHBITLENGTH);
            return _hashResult;
        }

        public new byte[] ComputeHash(byte[] data, int indexStart, int length)
        {
            if (_localState == null)
                throw new InvalidOperationException("HashAlgorithm not initialized.  Call Initialize first!");
            if (data == null)
                throw new ArgumentNullException("data");
            if (indexStart >= data.Length)
                throw new ArgumentOutOfRangeException("indexStart");
            if (length < 1)
                throw new ArgumentOutOfRangeException("length");
            if (indexStart + length > data.Length)
                throw new ArgumentOutOfRangeException("length");
            byte[] localData = new byte[length];
            Buffer.BlockCopy(data, indexStart, localData, 0, length);
            return ComputeHash(localData);
        }

        public new byte[] ComputeHash(System.IO.Stream buffer)
        {
            throw new NotImplementedException();
        }

        public override void Initialize()
        {
            _localState = SkeinCore.Initialize(WORDWIDTH, DEFAULTHASHBITLENGTH, null);
        }

        protected override void HashCore(byte[] array, int ibStart, int cbSize)
        {
            throw new NotImplementedException();
        }

        protected override byte[] HashFinal()
        {
            throw new NotImplementedException();
        }
    }
}
